package mao.leetcode.others;

public class leetcode424 {

    public static void main(String[] args) {
        leetcode424 leetcode424 = new leetcode424();
        leetcode424.characterReplacement("AABABBA",1);
    }

    public int characterReplacement(String s, int k) {
        if( s == null) return 0;
        int charMax = 0,left = 0,right=0;
        char[] chars = s.toCharArray();
        int[] map = new int[26];
        int temp = 0;
        while(right < chars.length){
            while(right<chars.length && charMax+k > right-left){
                temp = (++map[chars[right]-'A']);
                charMax=Math.max(charMax,temp);//charMax就是只增加不减少，即使是平移了。
                right++;
            }
            if(right == chars.length) break;
            map[chars[left]-'A']--;
            left++;
            temp = ++(map[chars[right]-'A']);
            charMax=Math.max(charMax,temp);//charMax就是只增加不减少，即使是平移了。
            right++;
        }
        return right - left;
    }
}

